package tasknow.utils;

import java.util.Calendar;
import java.util.Comparator;

public class Comparators {

	//@author A0096638M

	public static class DescriptionComparator implements Comparator<Task> {
		@Override
		public int compare(Task taskOne, Task taskTwo) {
			return taskOne.getDescription().compareTo(taskTwo.getDescription());
		}
	}

	public static class ComparatorStartDate implements Comparator<Task> {
		@Override
		public int compare(Task taskOne, Task taskTwo) {

			if (taskOne.getStartDate() == null
					&& taskTwo.getStartDate() == null) {
				return Constants.SAME;
			} else if (taskTwo.getStartDate() == null) {
				return Constants.BEFORE;
			} else if (taskOne.getStartDate() == null) {
				return Constants.AFTER;
			}

			int taskOneDayOfYear = taskOne.getStartDate().get(
					Calendar.DAY_OF_YEAR);
			int taskOneHour = taskOne.getStartDate().get(Calendar.HOUR_OF_DAY);
			int taskOneMinute = taskOne.getStartDate().get(Calendar.MINUTE);
			int taskTwoDayOfYear = taskTwo.getStartDate().get(
					Calendar.DAY_OF_YEAR);
			int taskTwoHour = taskTwo.getStartDate().get(Calendar.HOUR_OF_DAY);
			int taskTwoMinute = taskTwo.getStartDate().get(Calendar.MINUTE);

			if (taskOneDayOfYear > taskTwoDayOfYear) {
				return Constants.AFTER;
			} else if (taskOneDayOfYear < taskTwoDayOfYear) {
				return Constants.BEFORE;
			}

			if (taskOneHour > taskTwoHour) {
				return Constants.AFTER;
			} else if (taskOneHour < taskTwoHour) {
				return Constants.BEFORE;
			}

			if (taskOneMinute > taskTwoMinute) {
				return Constants.AFTER;
			} else if (taskOneMinute < taskTwoMinute) {
				return Constants.BEFORE;
			}

			return Constants.SAME;
		}
	}

	public static class ComparatorEndDate implements Comparator<Task> {
		@Override
		public int compare(Task taskOne, Task taskTwo) {
			if (taskOne.getEndDate() == null && taskTwo.getEndDate() == null) {
				return Constants.SAME;
			} else if (taskTwo.getEndDate() == null) {
				return Constants.BEFORE;
			} else if (taskOne.getEndDate() == null) {
				return Constants.AFTER;
			}

			int taskOneDayOfYear = taskOne.getEndDate().get(
					Calendar.DAY_OF_YEAR);
			int taskOneHour = taskOne.getEndDate().get(Calendar.HOUR_OF_DAY);
			int taskOneMinute = taskOne.getEndDate().get(Calendar.MINUTE);
			int taskTwoDayOfYear = taskTwo.getEndDate().get(
					Calendar.DAY_OF_YEAR);
			int taskTwoHour = taskTwo.getEndDate().get(Calendar.HOUR_OF_DAY);
			int taskTwoMinute = taskTwo.getEndDate().get(Calendar.MINUTE);

			if (taskOneDayOfYear > taskTwoDayOfYear) {
				return Constants.AFTER;
			} else if (taskOneDayOfYear < taskTwoDayOfYear) {
				return Constants.BEFORE;
			}

			if (taskOneHour > taskTwoHour) {
				return Constants.AFTER;
			} else if (taskOneHour < taskTwoHour) {
				return Constants.BEFORE;
			}

			if (taskOneMinute > taskTwoMinute) {
				return Constants.AFTER;
			} else if (taskOneMinute < taskTwoMinute) {
				return Constants.BEFORE;
			}

			return Constants.SAME;
		}
	}
	
}
